package com.awspaas.user.apps.sts.util;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import jodd.util.StringUtil;

import java.util.List;

/**
 * @Title: UitlIs
 * @Package com.awspaas.user.apps.sts.Util.UitlIs
 * @Description: 提交，保存按钮触发动作后各判断逻辑辅助类
 * @Author zhoujianchen
 * @Email
 * @Date 2022/9/04
 */
public class UitlIs {
    /**
     * 判断是否在控制范围之内
     * @param processDefId
     * @return
     */
    public boolean IsTransact(String processDefId) {
        int conuts = DBSql.getInt("select count(*) from BO_PS_PROCESS_SETTING_INFO  where PROCESSDEFNO=?",new Object[]{processDefId});
        if (conuts!=0){
            return false;
        }else {
            return true;
        }
    }
    /**
     * 判断该日志中间表中是否存在提交数据（提交需先判断业务日志表其他按钮数据是否通过）
     * @param taskInstId
     * @return
     */
    public boolean IsTransactDeal(String taskInstId,String bpmButtonId,String activityDefId) {
        //判断业务日志表其他按钮数据是否通过
        boolean isPass = IsOtherDeal(taskInstId,bpmButtonId,activityDefId);
        //判断业务表内是否有提交数据
        if (isPass){
            int conuts = DBSql.getInt("select count(*) from BO_PS_MIDDLE_OPERATION_LOG where TASK_INSTID = ? and BPMBUTTONNAME =?",new Object[]{taskInstId,bpmButtonId});
            if (conuts==0){
                return false;
            }else {
                return true;
            }
        }else {
            return true;
        }

    }
    /**
     * 判断业务日志表其他按钮数据是否通过
     * @param taskInstId
     * @return
     */
    public boolean IsOtherDeal(String taskInstId,String bpmButtonId,String activityDefId){

        boolean isPass = false;

        List<RowMap> rowMaps = DBSql.getMaps("select * from BO_PS_BUSINESS_BUTTON_REGISTER where ACTIVITY_DEFID = ? and BUTTONTYPE =?",new Object[]{activityDefId,"0"});
        if (rowMaps!=null&&rowMaps.size()!=0) {
            System.out.println("提交外其他按钮"+rowMaps);
            for (RowMap rowMap : rowMaps) {
                String otherAppButtonId = rowMap.getString("APPBUTTONID");
                String otherBPMButtonId = rowMap.getString("BUTTONID");
                int conuts = DBSql.
                        getInt("select count(*) from BO_PS_MIDDLE_OPERATION_LOG where BUTTONID =?and BPMBUTTONNAME =?" +
                                "and TASK_INSTID=? and BUTTONSTATUS=?", new Object[]{otherAppButtonId, otherBPMButtonId, taskInstId, "ok"});
                if (conuts != 0) {
                    isPass = true;
                }

            }
        }else {
            isPass = true;
            System.out.println("1:"+isPass);
        }

        return  isPass;
    }
    /**
     * 判断该日志中间表中是否存在提交数据
     * @param taskInstId
     * @return
     */
    public boolean IsNotTransact(String taskInstId,String bpmButtonId,String activityDefId) {
        //判断业务日志表其他按钮数据是否通过
        boolean isPass = IsOtherHasResult(taskInstId,bpmButtonId,activityDefId);
        //判断业务表内是否有提交数据
        if (isPass){
            int conuts = DBSql.getInt("select count(*) from BO_PS_MIDDLE_OPERATION_LOG where TASK_INSTID = ? and BPMBUTTONNAME =?",new Object[]{taskInstId,bpmButtonId});
            if (conuts==0){
                return false;
            }else {
                return true;
            }
        }else {
            return isPass;
        }

    }
    /**
     * 判断业务日志表其他按钮数据是否有日志记录
     * @param taskInstId
     * @return
     */
    public boolean IsOtherHasResult(String taskInstId,String bpmButtonId,String activityDefId){

        boolean otherHasResult = false;

        List<RowMap> rowMaps = DBSql.getMaps("select * from BO_PS_BUSINESS_BUTTON_REGISTER where ACTIVITY_DEFID = ? and BUTTONTYPE =?",new Object[]{activityDefId,"0"});
        if (rowMaps!=null&&rowMaps.size()!=0) {
            System.out.println("其他按钮外其他按钮"+rowMaps);
            for (RowMap rowMap : rowMaps) {
                String otherAppButtonId = rowMap.getString("APPBUTTONID");
                String otherBPMButtonId = rowMap.getString("BPMBUTTONNAME");
                List<RowMap> otherBPMButtonLogs = DBSql.
                        getMaps("select * from BO_PS_MIDDLE_OPERATION_LOG where BUTTONID =?and BPMBUTTONNAME =?" +
                                "and TASK_INSTID=?", new Object[]{otherAppButtonId, otherBPMButtonId, taskInstId});
                if (null!=otherBPMButtonLogs && !otherBPMButtonLogs.isEmpty()) {
                    for (RowMap otherBPMButtonLog : otherBPMButtonLogs) {
                        String buttonstatus = otherBPMButtonLog.getString("BUTTONSTATUS");
                        if ("".equals(buttonstatus)||"null".equals(buttonstatus) || null!=buttonstatus) {
                            otherHasResult = false;
                            return otherHasResult;
                        } else {
                            otherHasResult = true;
                        }
                    }
                }
            }
        }else {
            otherHasResult = true;
            System.out.println("1:"+otherHasResult);
        }

        return  otherHasResult;
    }


    /**
     * 判断该日志中间表中是否存在点击按钮事件
     * @param taskInstId
     * @param btnId
     * @return
     */
    public boolean IsOtherBtnDeal(String taskInstId, String btnId) {
        int conuts = DBSql.getInt("select count(*) from BO_PS_MIDDLE_OPERATION_LOG where TASK_INSTID = ? and BUTTONID =?",new Object[]{taskInstId,btnId});
        if (conuts==0){
            return false;
        }else {
            return true;
        }
    }

    /**
     * 判断该日志中间表中是否通过（提交需先判断业务日志表其他按钮数据是否通过）
     * @param taskInstId
     * @param btnId
     * @return
     */
    public boolean IsTransactPass(String taskInstId,String btnId,String activityDefId) {
        //判断业务日志表其他按钮数据是否通过
        boolean isPass = IsOtherDeal(taskInstId,btnId,activityDefId);
        System.out.println(isPass);
        //判断业务表内是否是否通过
        if (isPass){
            int conuts = DBSql.getInt("select count(*) from BO_PS_MIDDLE_OPERATION_LOG where TASK_INSTID = ? and BPMBUTTONNAME =? and BUTTONSTATUS =?",new Object[]{taskInstId,btnId,"ok"});
            if (conuts==0){
                System.out.println(1);
                return false;
            }else {
                System.out.println(2);
                return true;
            }
        }else {
            return isPass;
        }
    }

    /**
     * 是否通过
     * @param taskInstId
     * @param btnId
     * @return
     */
    public boolean IsPass(String taskInstId, String btnId,String activityDefId) {
        boolean isPass = false;
        RowMap rowMap = DBSql.getMap("select * from BO_PS_BUSINESS_BUTTON_REGISTER where ACTIVITY_DEFID = ? and BUTTONID =?",new Object[]{activityDefId,btnId});
        String appButtonId = rowMap.getString("APPBUTTONID");
        int conuts = DBSql.
                getInt("select count(*) from BO_PS_MIDDLE_OPERATION_LOG where BUTTONID =? and BPMBUTTONNAME =?" +
                        "and TASK_INSTID=? and BUTTONSTATUS=?",new Object[]{appButtonId,btnId,taskInstId,"ok"});
        if (conuts!=0) {
            isPass = true;
        }
        return isPass;
    }
    /**
     *  判断按钮状态是否为空
     * @param taskInstId
     * @param btnId
     * @return
     */
    public boolean isStateNull(String taskInstId, String btnId) {
        boolean isPass = true;
        List<RowMap> rowMapList = DBSql.getMaps("select * from BO_PS_MIDDLE_OPERATION_LOG where TASK_INSTID = ? and  BPMBUTTONNAME = ?", new Object[]{taskInstId, btnId});
        if (null!=rowMapList && !rowMapList.isEmpty()) {
            for (RowMap map : rowMapList) {
                System.out.println("流程状态为："+map.getString("BUTTONSTATUS"));
                if ("".equals(map.getString("BUTTONSTATUS"))||"null".equals(map.getString("BUTTONSTATUS"))) {
                    isPass = true;
                }else{
                    isPass = false;
                }
            }
        } else {
            isPass = false;
        }
        return isPass;
    }
    /**
     *  返回未结束办理的业务按钮消息
     * @param taskInstId
     * @return
     */
    public String getMsg(String taskInstId,String bpmButtonId,String activityDefId) {
        String msg = "";
        boolean isPass = true;
        List<RowMap> rowMaps = DBSql.getMaps("select * from BO_PS_BUSINESS_BUTTON_REGISTER where ACTIVITY_DEFID = ? and BUTTONTYPE =?", new Object[]{activityDefId, "0"});
        if (rowMaps!=null&&rowMaps.size()!=0){
            for (RowMap rowMap : rowMaps) {
                String appButtonId = rowMap.getString("APPBUTTONID");
                String btnId = rowMap.getString("BUTTONID");
                int conuts = DBSql.
                        getInt("select count(*) from BO_PS_MIDDLE_OPERATION_LOG where BUTTONID = ? and BPMBUTTONNAME = ?" +
                                "and TASK_INSTID= ? and BUTTONSTATUS= ?", new Object[]{appButtonId, btnId, taskInstId, "ok"});
                String btnName = "";
                if (conuts == 0) {
                    isPass = false;
                    RowMap rowMap1 = DBSql.
                            getMap("select * from BO_PS_BUSINESS_BUTTON_REGISTER where ACTIVITY_DEFID = ? and BUTTONID = ?"
                                    , new Object[]{activityDefId, btnId});
                    btnName = rowMap1.getString("BUTTONNAME");
                }
                if (isPass == false) {
                    msg = msg + btnName;

                }
            }
    }
        msg =msg+"等按钮办理未通过，请通过后再进行提交";
        return  msg;
    }

    /**
     * 判断驳回按钮状态是否为空（先判断其他业务按钮是否有未处理完的数据）
     * @param taskInstID
     * @param bpmButtonID
     * @param activityDefId
     * @return
     */
    public boolean IsRefuseDeal(String taskInstID, String bpmButtonID, String activityDefId) {
        //判断业务日志表其他按钮数据是否存在未处理完的数据
        boolean isPass = IsOtherNullDeal(taskInstID,bpmButtonID,activityDefId);
        if (isPass){
            //判断驳回数据是否为空
            int conuts = DBSql.getInt("select count(*) from BO_PS_MIDDLE_OPERATION_LOG where TASK_INSTID = ? and BPMBUTTONID =?",new Object[]{taskInstID,bpmButtonID});
            System.out.println("驳回数据为："+conuts+"条");
            if (conuts==0){
                return false;
            }else {
                return true;
            }
        }else {
            return true;
        }
    }

    /**
     * 判断其他业务按钮是否有其他未处理完的数据
     * @param taskInstID
     * @param bpmButtonID
     * @param activityDefId
     * @return
     */
    public boolean IsOtherNullDeal(String taskInstID, String bpmButtonID, String activityDefId) {
        boolean isPass = true;

        List<RowMap> rowMaps = DBSql.getMaps("select * from BO_PS_BUSINESS_BUTTON_REGISTER where ACTIVITY_DEFID = ? and BUTTONTYPE =?",new Object[]{activityDefId,"0"});
        if (rowMaps!=null&&rowMaps.size()!=0) {
            System.out.println("其他业务按钮" + rowMaps);
            for (RowMap rowMap : rowMaps) {
                String appButtonId = rowMap.getString("APPBUTTONID");
                String btnId = rowMap.getString("BUTTONID");
                List<RowMap> rowMaps1 = DBSql.
                        getMaps("select * from BO_PS_MIDDLE_OPERATION_LOG where BUTTONID =?and BPMBUTTONNAME =?" +
                                "and TASK_INSTID=?", new Object[]{appButtonId, btnId, taskInstID});
                if (rowMaps1!=null&&rowMaps1.size()>0) {
                    for (RowMap map : rowMaps1) {
                        if ("".equals(map.getString("BUTTONSTATUS"))
                                || "NULL".equals(map.getString("BUTTONSTATUS"))){
                            isPass = false;
                        }

                    }
                }
            }

        }
        return isPass;

    }

    /**
     * 返回其他业务按钮是否有其他未处理完的数据按钮信息
     * @param taskInstID
     * @param bpmButtonID
     * @param activityDefId
     * @return
     */
    public String getOtherNullMsg(String taskInstID, String bpmButtonID, String activityDefId) {
        boolean isPass = true;
        String msg = "";
        List<RowMap> rowMaps = DBSql.getMaps("select * from BO_PS_BUSINESS_BUTTON_REGISTER where ACTIVITY_DEFID = ? and BUTTONTYPE =?",new Object[]{activityDefId,"0"});
        if (rowMaps!=null&&rowMaps.size()!=0) {
            System.out.println("其他业务按钮" + rowMaps);
            for (RowMap rowMap : rowMaps) {
                String appButtonId = rowMap.getString("APPBUTTONID");
                String btnId = rowMap.getString("BUTTONID");
                List<RowMap> rowMaps1 = DBSql.
                        getMaps("select * from BO_PS_MIDDLE_OPERATION_LOG where BUTTONID =?and BPMBUTTONNAME =?" +
                                "and TASK_INSTID=?", new Object[]{appButtonId, btnId, taskInstID});
                if (rowMaps1!=null&&rowMaps1.size()>0) {
                    for (RowMap map : rowMaps1) {
                        String btnName = "";
                        if ("".equals(map.getString("BUTTONSTATUS"))
                                || "NULL".equals(map.getString("BUTTONSTATUS"))){
                            btnName = rowMap.getString("BUTTONNAME");
                            msg = msg + btnName;
                        }

                    }
                }
            }

        }
        msg =msg+"等按钮办理未处理完成，请处理后再进行操作";
        return  msg;

    }

    /**
     * 判断该日志中间表中是否通过（提交需先判断业务日志表其他按钮数据是否通过）
     * @param taskInstId
     * @param btnId
     * @return
     */
    public boolean IsRefuePass(String taskInstId,String btnId,String activityDefId) {
        //判断业务日志表其他按钮数据是否存在未办理结束的情况
        boolean isPass = IsOtherNullDeal(taskInstId,btnId,activityDefId);
        System.out.println(isPass);
        //判断业务表内是否是否通过
        if (isPass){
            int conuts = DBSql.getInt("select count(*) from BO_PS_MIDDLE_OPERATION_LOG where TASK_INSTID = ? and BPMBUTTONNAME =? and BUTTONSTATUS =?",new Object[]{taskInstId,btnId,"ok"});
            if (conuts==0){
                System.out.println(1);
                return false;
            }else {
                System.out.println(2);
                return true;
            }
        }else {
            return isPass;
        }
    }

    /**
     * 判断业务状态是否符合
     * @param taskInstId
     * @param activityDefId
     * @return
     */
    public boolean isBussionStatus(String taskInstId,String btnId,String activityDefId){
        //获取配置信息
        String bussinessStatusId = DBSql.getString("select  BUSINESSSTATUSID from BO_PS_BUSINESS_ACTIVITY where ACTIVITY_DEFID =?",new Object[]{activityDefId});
        //判断是否配置
        boolean isTrue = false;
        if (StringUtil.isNotEmpty(bussinessStatusId)) {
            //获取按钮通过后返回业务状态
            String btnBussStauts = DBSql.getString("select BUSINESSSTATUSID from BO_PS_MIDDLE_OPERATION_LOG where TASK_INSTID =? and BPMBUTTONNAME = ?",new Object[]{taskInstId,btnId});
            //判断返回是否为空
            if(StringUtil.isNotEmpty(btnBussStauts)){
                 //不为空判断两个业务状态是否相同
                if (btnBussStauts.equals(bussinessStatusId)){
                    //相同通过
                    isTrue = true;
                }
            }else {
                //为空不通过
                isTrue = false;
            }

        } else {
            //未配置直接通过
            isTrue = true;
        }
        return  isTrue;

    }
    /**
     * 返回提示信息
     * @param taskInstId
     * @param activityDefId
     * @return
     */
    public String reBussionStatus(String taskInstId,String btnId,String activityDefId){
        //获取配置信息
        String bussinessStatusId = DBSql.getString("select  BUSINESSSTATUSID from BO_PS_BUSINESS_ACTIVITY where ACTIVITY_DEFID =?",new Object[]{activityDefId});
        //判断是否配置
        // boolean isTrue = false;
        String msg = "";
        if (StringUtil.isNotEmpty(bussinessStatusId)) {
            //获取按钮通过后返回业务状态
            String btnBussStauts = DBSql.getString("select BUSINESSSTATUSID from BO_PS_MIDDLE_OPERATION_LOG where TASK_INSTID =? and BPMBUTTONNAME = ?",new Object[]{taskInstId,btnId});
            BO bo = SDK.getBOAPI().query("BO_PS_MIDDLE_OPERATION_LOG").addQuery("TASK_INSTID =",taskInstId).addQuery("BPMBUTTONNAME =",btnId).detail();
            //判断返回是否为空
            if(StringUtil.isNotEmpty(btnBussStauts)){
                //不为空判断两个业务状态是否相同
                if (!btnBussStauts.equals(bussinessStatusId)){
                    //相同通过
                    msg = "业务状态与配置项不同，配置项为"+bussinessStatusId+",返回业务状态为"+btnBussStauts+",审批不通过";
                    bo.set("BUSINESSSTATUSRESULT","FALSE");
                }else{
                    bo.set("BUSINESSSTATUSRESULT","TRUE");
                }
            }else {
                //为空不通过
               msg = "业务状态未传回 审批不通过";
               bo.set("BUSINESSSTATUSRESULT","TRUE");
            }
            SDK.getBOAPI().update("BUSINESSSTATUSRESULT",bo);

        } else {
            //未配置直接通过
           msg = "通过";
        }
        return  msg;

    }
}
